package com.source.baseData.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.baseData.entity.Stalls;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.source.labelAndBarcode.vo.StallsVo;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author Joey
 * @since 2021-02-10
 */
public interface StallsMapper extends BaseMapper<Stalls> {

    @Select(value = "SELECT\n" +
            "stalls.*,\n" +
            "slaughterHouseName,\n" +
            "su.`name`,\n" +
            "su.qq,\n" +
            "su.wechat,\n" +
            "su.wechatCode,\n" +
            "su.phone,\n" +
            "su.mobile\n" +
            "FROM stalls, slaughter_house sh, sys_user su \n" +
            "WHERE stalls.slaughterHouseId = sh.slaughterHouseId AND su.userId = stalls.userId AND stalls.isDelete = 0 ")
    List<StallsVo> findAll();

    @Select(value = "SELECT\n" +
            "stalls.*,\n" +
            "slaughterHouseName,\n" +
            "su.`name` as `name`,\n" +
            "su.qq as qq,\n" +
            "su.wechat as wechat,\n" +
            "su.wechatCode as wechatCode,\n" +
            "su.phone as phone,\n" +
            "su.mobile as mobile\n" +
            "FROM stalls, slaughter_house sh, sys_user su \n" +
            "WHERE stalls.slaughterHouseId = sh.slaughterHouseId AND su.userId = stalls.userId AND stalls.isDelete = 0 ")
    IPage<StallsVo> findAllOnPagination(Page<StallsVo> page);

    @Select(value = "SELECT\n" +
            "stalls.*,\n" +
            "slaughterHouseName,\n" +
            "su.`name`,\n" +
            "su.qq,\n" +
            "su.wechat,\n" +
            "su.wechatCode,\n" +
            "su.phone,\n" +
            "su.mobile\n" +
            "FROM stalls, slaughter_house sh, sys_user su \n" +
            "WHERE stalls.slaughterHouseId = sh.slaughterHouseId AND su.userId = stalls.userId AND stalls.`status` = #{i} " +
            "AND stalls.isDelete = 0 ")
    List<StallsVo> findAllOnMultiStatus(int i);

    @Select(value = "SELECT\n" +
            "stalls.*,\n" +
            "slaughterHouseName,\n" +
            "su.`name`,\n" +
            "su.qq,\n" +
            "su.wechat,\n" +
            "su.wechatCode,\n" +
            "su.phone,\n" +
            "su.mobile\n" +
            "FROM stalls, slaughter_house sh, sys_user su \n" +
            "WHERE stalls.slaughterHouseId = sh.slaughterHouseId AND su.userId = stalls.userId AND stalls.`status` = #{i} " +
            "AND stalls.isDelete = 0 ")
    IPage<StallsVo> findAllOnMultiStatusAndPagination(Page<StallsVo> page, int i);

    @Select(value = "SELECT\n" +
            "stalls.*,\n" +
            "slaughterHouseName,\n" +
            "su.`name`,\n" +
            "su.qq,\n" +
            "su.wechat,\n" +
            "su.wechatCode,\n" +
            "su.phone,\n" +
            "su.mobile\n" +
            "FROM stalls, slaughter_house sh, sys_user su \n" +
            "WHERE stalls.slaughterHouseId = sh.slaughterHouseId AND su.userId = stalls.userId AND stalls.`status` = 1 " +
            "AND CONCAT(stallsName, slaughterHouseName, su.`name`, su.phone, su.mobile) Like #{name} " +
            "AND stalls.isDelete = 0 ")
    List<StallsVo> findByLikeName(String name);

    @Select(value = "SELECT\n" +
            "stalls.*,\n" +
            "slaughterHouseName,\n" +
            "su.`name`,\n" +
            "su.qq,\n" +
            "su.wechat,\n" +
            "su.wechatCode,\n" +
            "su.phone,\n" +
            "su.mobile\n" +
            "FROM stalls, slaughter_house sh, sys_user su \n" +
            "WHERE stalls.slaughterHouseId = sh.slaughterHouseId AND su.userId = stalls.userId AND stalls.`status` = 1 " +
            "AND CONCAT_WS(',',stallsName, slaughterHouseName, su.`name`, su.mobile) Like #{name} " +
            "AND stalls.isDelete = 0 ")
    IPage<StallsVo> findByLikeNameOnPagination(Page<StallsVo> page, String name);

    @Select(value = "SELECT\n" +
            "stalls.*,\n" +
            "slaughterHouseName,\n" +
            "su.`name`,\n" +
            "su.qq,\n" +
            "su.wechat,\n" +
            "su.wechatCode,\n" +
            "su.phone,\n" +
            "su.mobile\n" +
            "FROM stalls, slaughter_house sh, sys_user su \n" +
            "WHERE stalls.slaughterHouseId = sh.slaughterHouseId AND su.userId = stalls.userId " +
            "AND stalls.userId = #{userId} AND stalls.isDelete = 0 ")
    List<StallsVo> findByUserId(String userId);

    @Select(value = "SELECT\n" +
            "stalls.*,\n" +
            "slaughterHouseName,\n" +
            "su.`name`,\n" +
            "su.qq,\n" +
            "su.wechat,\n" +
            "su.wechatCode,\n" +
            "su.phone,\n" +
            "su.mobile\n" +
            "FROM stalls, slaughter_house sh, sys_user su \n" +
            "WHERE stalls.slaughterHouseId = sh.slaughterHouseId AND su.userId = stalls.userId" +
            "AND sh.slaughterHouseId = #{slaughterHouseId} AND stalls.isDelete = 0 ")
    List<StallsVo> findBySlaughterHouseId(String slaughterHouseId);

    @Select(value = "SELECT\n" +
            "stalls.*,\n" +
            "slaughterHouseName,\n" +
            "su.`name`,\n" +
            "su.qq,\n" +
            "su.wechat,\n" +
            "su.wechatCode,\n" +
            "su.phone,\n" +
            "su.mobile\n" +
            "FROM stalls, slaughter_house sh, sys_user su \n" +
            "WHERE stalls.slaughterHouseId = sh.slaughterHouseId AND su.userId = stalls.userId " +
            "AND stalls.userId = #{userId} AND stalls.isDelete = 0 ")
    IPage<StallsVo> findByUserIdOnPagination(Page<StallsVo> page, String userId);

    @Select(value = "SELECT\n" +
            "stalls.*,\n" +
            "slaughterHouseName,\n" +
            "su.`name`,\n" +
            "su.qq,\n" +
            "su.wechat,\n" +
            "su.wechatCode,\n" +
            "su.phone,\n" +
            "su.mobile\n" +
            "FROM stalls, slaughter_house sh, sys_user su \n" +
            "WHERE stalls.slaughterHouseId = sh.slaughterHouseId AND su.userId = stalls.userId " +
            "AND sh.slaughterHouseId = #{slaughterHouseId} AND stalls.isDelete = 0 ")
    IPage<StallsVo> findBySlaughterHouseIdOnPagination(Page<StallsVo> page, String slaughterHouseId);

    @Select(value = "SELECT\n" +
            "stalls.*,\n" +
            "slaughterHouseName,\n" +
            "su.`name`,\n" +
            "su.qq,\n" +
            "su.wechat,\n" +
            "su.wechatCode,\n" +
            "su.phone,\n" +
            "su.mobile\n" +
            "FROM stalls, slaughter_house sh, sys_user su \n" +
            "WHERE stalls.slaughterHouseId = sh.slaughterHouseId AND su.userId = stalls.userId " +
            "AND stallsId = #{id} AND stalls.isDelete = 0 ")
    StallsVo findById(String id);

    String SELECT = "SELECT\n" +
            "stalls.*,\n" +
            "slaughterHouseName,\n" +
            "su.`name`,\n" +
            "su.qq,\n" +
            "su.wechat,\n" +
            "su.wechatCode,\n" +
            "su.phone,\n" +
            "su.mobile\n" +
            "FROM stalls, slaughter_house sh, sys_user su \n" +
            "WHERE stalls.slaughterHouseId = sh.slaughterHouseId AND su.userId = stalls.userId " +
            "AND stalls.isDelete = 0 " ;

    @Select(SELECT + " AND ( stalls.`status` = 2 or stalls.`status` = 3 ) ")
    IPage<StallsVo> findByUnChecked(Page<StallsVo> page);

    @Select(SELECT + "AND ( stalls.`status` = 2 or stalls.`status` = 3 ) " +
            " AND CONCAT_WS(',',stallsName, slaughterHouseName, su.`name`, su.mobile) Like #{s}")
    IPage<StallsVo> findByUnCheckedAndKeyWord(String s, Page<StallsVo> page);
}
